<?php

class Fcategory {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty,$WPath) {
		$this->db = $db;
		$this->smarty = $smarty;
		$this->WPath = $WPath;
		$this->siteUrl = '?page=';
		
	}
	
	/* 
	 * Set default category 
	 */
	/*public function default_category() {
		if($_SERVER['QUERY_STRING']==""){
			$cat = FIN_categoryBean::get_default_category($this->db);
			if($cat->cat_parent!='0')
				header("Location: ?cat=".$cat->cat_parent."&subcat=".$cat->cat_id);	
			else	
				header("Location: ?cat=".$cat->cat_id);	
		}	
	}**/
	
	
	/* Input: 	-
	 * Return: 	Smarty-Variable 'MENUTOP', which contains the whole category
	 *			tree as a string, which is received from the category
	 *			database table. This string is in the form of a html list.
	 *			For a better understanding have a closer look to the 
	 *			description of tghe function build_li($cats).
	 */
	
	public function view_categoryfooter() {
		$cat = FIN_categoryBean::getCatbyURL($this->db,'services');
		if($cat!=''){
			foreach($cat as $item){
					$item->paralink = FOLDER_HOST.$item->parent_url.'/'.$item->cat_url;		
			}
		}
		$this->smarty->assign('MENUSERVICESFOOTER',$cat);	
	}
	
	public function view_bottom_category() {
		
		$cat = FIN_categoryBean::getHeaderCategoryByType($this->db,'3');
		$this->smarty->assign('MENUBOTTOM',$cat);	
	}
	 
	public function view_top_category() {
		
		$cat = FIN_categoryBean::getHeaderCategoryByType($this->db,'4');
		$this->smarty->assign('MENUTOP',$cat);	
	}
	
	public function view_top_link() {
		
		$cat = FIN_categoryBean::getHeaderCategoryByType($this->db,'1');
		$this->smarty->assign('TOPLINK',$cat);	
	}
	
	/* Input: 	a number representing the id of a given categoryparent.
	 * Return: 	the active category-items to the given parent $catpatent,
	 * 			the returned value $cat contains the whole table with
	 * 			all the columns of tab_category.
	 */
	public function get_top_category_tree($catparent) {
		$position = "cat_ontop";
		$cat = FIN_categoryBean::get_cat_by_condition($this->db,$catparent,$_SESSION["LC_ALL"],$position,'0');
		return $cat;
	}
	
	/* Input:	ezSql - list of all top menue items. 
	 * Return:	a string, which contains a unnumerated list of all 
	 * 			categories and subcategories. For a better understanding
	 *			have a closer look to the description of tghe function
	 *			build_li($cats) 
	*/
	public function build_top_menu_item($cats) 
	{ 
		if($cats!=""){
		   $str = '<ul>';
		   $str .= $this->build_li($cats);
		   $str .= '</ul>'; 
		   
		   return $str;
		} 
	}
	
	/* Input: 	a "well ordered" category table  
	 *			Format of the table is given by ezSql.
	 *			Child categories are 
	 *			
	 * Return:  a string containing html - code:
	 * 			this code contains a nested list structure of links
	 *			to categories and subcategories. These links are in 
	 *			the form: 
	 *			<li id='cat_id'>
	 *				<a href=?cat='cat_id' title='cat_description'>
	 *			 		<span class="textmenu">'cat_name'</span></li>
	 *				</a>
	 *					<ul>
	 *						...{same structure, inserted by recursion}
	 *					</ul>
	 *			</li>
	*/
	public function build_li($array)
	{
		$str = "";
		if(isset($_GET['cat']) && $_GET['cat']!="")
			$getcat = $_GET['cat'];
		$size_content = FgcmsConfigBean::getconfig($this->db,"FONT_SIZE_TOP_MENU");
		$size = $size_content->value;	
		$font_content = FgcmsConfigBean::getconfig($this->db,"FONT_FAMILY");
		$family = $font_content->value;
		foreach($array as $cat)
		{
			/* creates a html-link as item of a list: 
			 * the link itself is left blank at first an is given by 
			 * the general arbitrary expression {_link_}
			 */
			$active_menu=""; 
			if($getcat == $cat->cat_id)
				$active_menu = 'class="active"';
			if($cat->active == 1)
			{
				$str .= sprintf('<li id="%s" '.$active_menu.'>' .
						'<a {_link_} title="%s">' .
						'<span class="textmenu" style="font-size:'.$size.'px;font-family:'.$family.'">%s</span>' .
						'</a>',
						$cat->cat_id,
						$cat->cat_description,
						$cat->cat_name);
			}
			
			if($cat->sub != null)
			/* replace {_link_} by e.g. 'href=?cat=5' if cat_id is 5 in the $str.
			 * start a new sublist, if there are subcategories. This
			 * is done by recursion.
			 */			
			{
				$str = str_replace("{_link_}", sprintf('href="%s"', $this->build_link($cat)), $str);
				
				$str .= '<ul width="200">';
				$str .= $this->build_li($cat->sub);
				$str .= "</ul>";
			}
			else
			// replace only {_link_} by e.g. 'href=?cat=5' if cat_id is 5 in the $str.
			// No Recursion, because of no subcategories.
			{
				$str = str_replace("{_link_}", sprintf('href="%s"', $this->build_link($cat)), $str);
			}
			
			$str .= "</li>";
		}
		
		return $str;
	}
	
	/* Input: 	single data row of the category table in ezSql-format.
	 * Return: 	String: e.g. '?cat=5' if cat_id is 5.
	 */
	public function build_link($cat)
	{
		//TODO: build the link of menu item based on actual website structure
		return "?cat=" . $cat->cat_id ;
	}
	
	/* Input:	-
	 * Return:	Smarty Variable 'MENULEFT'. 
	 */
	public function view_left_category() {
		$catid = 0;
		if(isset($_GET['cat']) && $_GET['cat']!="")
			$catid = $_GET['cat'];
			
		$cat = $this->get_left_category_tree($catid,"2");
		$catparent = FIN_categoryBean::get_catname_by_id($this->db,$catid);
		$str = $this->build_left_menu_item($cat);
		$this->smarty->assign('MENUPARENT',$catparent->cat_name);	
		$this->smarty->assign('MENULEFT',$str);	
	}
	
	/* Input:	number representing the parent of a category
	 * Return: 	a "well ordered" category table in the ezSql-format.
	 *			"well ordered" means, that the output table is a nested structure 
	 *			of tables containing only items belonging to one parent.
	 *			Every table obtains an additional variable named 'sub', 
	 *			containing an analogous table of child categories...
	 */
	public function get_left_category_tree($catparent,$position) {
		$cat = FIN_categoryBean::get_cat_by_condition($this->db,$catparent,$_SESSION["LC_ALL"],$position,'0');
		if($cat == null) return;
		
		foreach($cat as $item)
		{
			$item->sub = $this->get_left_category_tree($item->cat_id,$position);
		}
		return $cat;
	}
	
	/* 
	 *
	*/
	public function build_left_menu_item($cats) { 
		
		if($cats!=""){
		   $str = '<ol id="root" style="display:inherit">';
		   $str .= $this->build_left_li($cats, 0);
		   $str .= '</ol>'; 
		   
		   return $str;
		}   
	}

	/* 
	 *
	*/
	public function build_left_li($array, $level){
		$str = "";
		$tab = "";
		if($level > 0)
		{
			$tab = str_repeat("", $level*2);
		}
		
		if(isset($_GET['subcat']) && $_GET['subcat']!="")
			$getcat = $_GET['subcat'];
		foreach($array as $cat)
		{
			$active_menu=""; 
			if($getcat == $cat->cat_id)
				$active_menu = 'active';
			if($cat->active == 1)
			{	
					$str .= sprintf('<li id="%s" >' .
							'<span class="_class_ '.$active_menu.'">' .
							'<a {_link_} title="%s">' .
							 $tab. '%s</a></span>',
							"left_item_" . $cat->cat_id,
							$cat->cat_description,
							$cat->cat_name);
			}	
			if($cat->sub != null)
			{
				$str = str_replace("_event_", "toggleSub('sub_$cat->cat_id')" ,$str);
				
				$str = str_replace("_class_", "itemwrap",$str);
				
				$str = str_replace("{_link_}", "", $str);
				
				$str .= "<ul id=\"sub_$cat->cat_id\" style=\"display:block\">";
				$str .= $this->build_left_li($cat->sub, $level+1);
				$str .= "</ul>";
			}
			else
			{
				$str = str_replace("_event_", "" , $str);
				$str = str_replace("_class_", "menu_item" ,$str);
				$str = str_replace("{_link_}", " href=\"" . $this->build_left_link($cat) . "\" ", $str);
			}
			$str .= "</li>";
			
		}
		return $str;
	}
	
	public function build_left_link($cat){
		//TODO: build the link of menu item base on actual website structure
			return $this->WPath."san-pham/".JKDocument::RewriteLink($cat->cat_id,$cat->cat_name)."/";
	}

	
	/*public function view_sitemap() {
		$cat = $this->get_left_category_tree(0,"");
		$page->page_content = '<div class="tree_site">';
		$page->page_content .= $this->build_left_menu_item($cat);
		$page->page_content .= '</div>';
		$page->page_name = "Sitemap";
		$this->smarty->assign('PAGE',$page);	
	}*/
	
	
	/* Input:	number representing the parent of a category
	 * Return: 	a "well ordered" category table in the ezSql-format.
	 *			"well ordered" means, that the output table is a nested structure 
	 *			of tables containing only items belonging to one parent.
	 *			Every table obtains an additional variable named 'sub', 
	 *			containing an analogous table of child categories...
	 */
	public function get_menubar_category($catparent,$position) {
		$cat = FIN_categoryBean::get_cat_by_condition($this->db,$catparent,$_SESSION["LC_ALL"],$position,'0');
		if($cat == null) return;

		foreach($cat as $item)
		{
			$item->sub = $this->get_menubar_category($item->cat_id,$position);
		}
		return $cat;
	}
	
	/* 
		View ALl categories with menu bar  
	 */
	
	public function view_menubar() {
		$cat = $this->get_menubar_category("","");
		$this->smarty->assign('MENUBAR',$cat);
	}
	
}
?>
